package pira.db.ui;

import java.util.Map;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.IndexManager;
import org.neo4j.kernel.EmbeddedGraphDatabase;

import pira.elements.Author;
import pira.elements.Paper;

public abstract class Neo4jDBUI {
	// transaction for database
	public Transaction tx;

	// graph database service
	protected GraphDatabaseService graphDb;

	// whole node number
	public long allNode;

	// database index
	public IndexManager index;

	//
	public static final String EXTERNAL = "EXTERNALNODE";
	public static final String NOT_EXIST = "Not Exist!";

	// the range for the calculation of H-Index
	public static final long BEGINAUTHORID = 0;
	public static final long ENDAUTHORID = 527248;
	
	public Neo4jDBUI(String defaultDatabase, String config) {
		Map<String, String> configuration = EmbeddedGraphDatabase
				.loadConfigurations(config);
		graphDb = new EmbeddedGraphDatabase(defaultDatabase, configuration);
		tx = graphDb.beginTx();
		try {
			index = graphDb.index();
			tx.success();
		} finally {
			tx.finish();
		}
	}

	protected abstract long addPaperToDB(Paper paper);
	protected abstract long addAuthorToDB(Author author);
	
	public GraphDatabaseService getGraphDatabaseService() {
		return this.graphDb;
	}

	public void shutDown() {
		this.graphDb.shutdown();
	}
}
